package com.gs.m_person_details.persistent

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.BaseStateCallback
import com.gs.common.constant.StateCode
import com.gs.m_person_details.constant.UpdateErrorStateCode
import com.gs.m_person_details.model.ChangeInfoModel

class ChangeInfoPersistent(context: Context) {

    private val changeInfoModel by lazy { ChangeInfoModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun updateUserInfo(username: String, changeNickname: String? = null,
                       changeUsername: String? = null, changePhoneNumber: String? = null,
                       callback: OnDoUpdateUserInfoStateCallback) {
        Thread{
            changeInfoModel.updateUserInfo(username, changeNickname, changeUsername, changePhoneNumber) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onUpdateUserInfoLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onUpdateUserInfoSuccess(changeNickname, changeUsername, changePhoneNumber)
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onUpdateUserInfoFailed()
                        }
                    }
                    UpdateErrorStateCode.STATE_USERNAME_EXISTED -> {
                        handler.post {
                            callback.onUsernameExistedError()
                        }
                    }
                    UpdateErrorStateCode.STATE_PHONE_NUMBER_EXISTED -> {
                        handler.post {
                            callback.onPhoneNumberExistedError()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    interface OnDoUpdateUserInfoStateCallback: BaseStateCallback{
        fun onUpdateUserInfoLoading(){}
        fun onUpdateUserInfoSuccess(changeNickname: String? = null,
                                    changeUsername: String? = null, changePhoneNumber: String? = null)
        fun onUpdateUserInfoFailed()
        fun onUsernameExistedError()
        fun onPhoneNumberExistedError()
    }

    companion object{
        private var instance: ChangeInfoPersistent? = null

        fun getInstance(context: Context): ChangeInfoPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = ChangeInfoPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}